<?php

// Demo SMS Receiver
 
//include the api for sending and receiving
include_once 'AppZoneSender.php';
include_once 'AppZoneReciever.php';

try{
	//create the receiver
	$reciever=new AppZoneReciever();
	//getting the message content
	$content = $reciever->getMessage(); // get the message content
    $address = $reciever->getAddress(); // get the sender's address
    $correlationId = $reciever->getCorrelator(); // get the correlation id of the messgae
   
    $responceMsg;	
	//your logic goes here......
	$split = split(' ', $content);	
	$responceMsg = bmiLogicHere($split);
		
	/*
	creating the sender object with 
		server url
		appId
		password
	*/
	$sender=new AppZoneSender("http://127.0.0.1:8000/sms/", "appid", "password");
	
	//sending a one message
	$res=$sender->sms($responceMsg, $reciever->getAddress());		
}
catch(AppZoneException $ex){
	//throws when failed sending or receiving the sms
	logFile("ERROR: {$ex->getStatusCode()} | {$ex->getStatusMessage()}");
}

function bmiLogicHere($split){
	$responceMsg;
    if (sizeof($split) < 2)
    {
    	$responceMsg = "Invalid message content";
    }
    else
    {
       $weight = (float) $split[0]."<br/>";
       $height = (float) $split[1]."<br/>";
	   	   
       $bmi = getBMIValue($weight, ($height / 100));
	   $category = getCategory($bmi);
	   	          
       $responceMsg = "Your BMI :". round($bmi,2).", Category :".$category;	   
     }
     return $responceMsg;
}

function getBMIValue($weight, $height)
{		
	return ($weight / pow($height, 2));
}

function getCategory($bmiValue)
{		
	if ($bmiValue < 18.5)
	{
		return "Underweight";
	}
	else if ($bmiValue >= 18.5 && $bmiValue < 24.9)
	{
		return "Normal Weight";
	}
	else if ($bmiValue >= 25 && $bmiValue < 29.9)
	{
		return "Overweight";
	}
	else
	{
		return "Obesity";
	}
}
?>

